//
//  PersonInforVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/8.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "PersonInforVC.h"
#import "HDPicModle.h"
#import "GetPhoto.h"
#import "ChangePhoneVC.h"
#import "ChangePassWordVC.h"
#import "CancellationVC.h"
#import "UIImage+HDExtension.h"
/**  极光  **/
#import "JPUSHService.h"
#import "SafePhoneCodeController.h"

@interface PersonInforVC ()
@property (strong, nonatomic) CJButton *btnLogOut;
@property (strong, nonatomic)  FlexFrameView *viewHeaderBg;
@property (strong, nonatomic)  UIImageView *imgVHeader;
@property (copy, nonatomic) NSArray *arrTitles;
@property (strong, nonatomic) NSArray *arrContent;
@end

@implementation PersonInforVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"个人信息")];
    [self setBackLeftButton:@""];
    [self setRightImage:CJLString(@"注销") image:@""];
    [self createTopView];
    [self createUI];
    [self setTapAction];
}

- (void)createTopView{
    CGRect rcFrame = CGRectMake(0, kTopHeight, [[UIScreen mainScreen] bounds].size.width, 0);
    self.viewHeaderBg = [[FlexFrameView alloc] initWithFlex:@"PersonInforTopView" Frame:rcFrame Owner:self];
    self.viewHeaderBg.flexibleHeight = YES;

    [self.viewHeaderBg layoutIfNeeded];
    [self.view addSubview:self.viewHeaderBg];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self setWhiterColorAppNavigationBarStyle];
    [self getUSerInfo];
}

- (void)RightTextClick:(UIButton *)sender{
    CancellationVC *vc = [CancellationVC new];
    UITextField *field2 = [self.view viewWithTag:1001];
    vc.isCancellFail = NO;
    vc.strPhoneNum = field2.text;
    [self pushVc:vc];
}

- (void)getUSerInfo{
    
    WS(ws);
    UITextField *field1 = [self.view viewWithTag:1000];
    UITextField *field2 = [self.view viewWithTag:1001];
    [CJNetWork getPartyUserCenterInfoSuccess:^(id responseObject) {
    NSDictionary *dic = (NSDictionary *)responseObject;
        field1.text = [CJControl getThreeUnaryValueStr:dic[@"name"]];
        field2.text = [CJControl getThreeUnaryValueStr:dic[@"phone"]];

        if ([CJRegular getIsStrEmpty:[CJControl getThreeUnaryValueStr:dic[@"headImg"]]]) {
            ws.imgVHeader.image = [UIImage hd_captureCircleImageWithImage:[UIImage imageNamed:@"zcj_default_person_icon"] andBorderWith:0 andBorderColor:[UIColor cjClearColor]];
        }else{
            ws.imgVHeader.image = [UIImage hd_captureCircleImageWithURL:[CJControl getThreeUnaryValueStr:dic[@"headImg"]] andBorderWith:0 andBorderColor:[UIColor cjClearColor]];
        }
    }];
    
}

- (void)createUI{
    
    [self.view addSubview:self.btnLogOut];
    
    for (int i = 0; i < self.arrTitles.count; i++) {
        
        UIView * liebiaoView = [[UIView alloc]initWithFrame:CGRectMake(0, self.viewHeaderBg.bottom + i * LW(50), SCREEN_WIDTH, LW(50))];
        liebiaoView.backgroundColor = [UIColor cjWhiteColor];
        [self.view addSubview:liebiaoView];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapBackView:)];
        liebiaoView.tag = i+100;
        liebiaoView.userInteractionEnabled = YES;
        [liebiaoView addGestureRecognizer:tap];

        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(CJToLeftMargin, LW(5), LW(120), LW(40))];
        label.text = self.arrTitles[i];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont cjTitleFont15];
        label.textColor = [UIColor cjMainTextColor];
        [liebiaoView addSubview:label];
        
        UITextField *textField = [UITextField textFieldWithFrame:CGRectMake(label.right, label.top, SCREEN_WIDTH - CJToLeftMargin-label.width - LW(25), LW(40)) placeholder:self.arrContent[i] backgroundColor:[UIColor cjClearColor] borderColor:nil cornerRadius:0 font:[UIFont cjTitleFont15] textColor:[UIColor cjMainTextColor] textAlignment:NSTextAlignmentRight text:@"" delegate:nil];
        textField.tag = 1000+ i;
        [liebiaoView addSubview:textField];
        textField.enabled = NO;
        
        UIImageView * right = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-CJToLeftMargin, LW(20), LW(6), LW(10))];
        right.image = [UIImage imageNamed:@"zcj_arrow_right"];
        right.contentMode = UIViewContentModeScaleAspectFit;
        [liebiaoView addSubview:right];
        if (i == 0) {
               right.hidden = YES;
           }
        
        UILabel * bg = [[UILabel alloc]initWithFrame:CGRectMake(CJToLeftMargin, liebiaoView.height -1, SCREEN_WIDTH, 1)];
        bg.backgroundColor = [UIColor cjBackgroundColor];
        [liebiaoView addSubview:bg];
        
    }
}

- (void)OnTapBackView:(UITapGestureRecognizer *)gesture {
      UITextField *field2 = [self.view viewWithTag:1001];
    
    if (gesture.view.tag == 100) {
    
    }else if (gesture.view.tag == 101){
        ChangePhoneVC *vc = [ChangePhoneVC new];
        vc.strPhone = field2.text;
        [self pushVc:vc];
    }else{
        SafePhoneCodeController *vc = [SafePhoneCodeController new];
        [self pushVc:vc];
    }
}

/// 点击事件
- (void)setTapAction{
    
    WS(ws);
    [self.viewHeaderBg setTapActionWithBlock:^{
        [ws changeUserHeaderImg];
    }];
}

- (void)changeUserHeaderImg{
    
    WS(ws);
    [GetPhoto getPhoto:self completion:^(UIImage *curImage, NSData *imageData) {

        [ws upLoadImageUImage:curImage withNSData:imageData];
        
    }];
}

- (void)upLoadImageUImage:(UIImage *)img withNSData:(NSData *)imageData{

    WS(ws);
    [CJNetWork upLoadImageUImage:img withNSData:imageData Success:^(id  _Nullable responseObject) {
//        ws.imgVHeader.image = img;
        [ws changePersonInfowithString:(NSString *)responseObject];
    }];
    
}


- (void)changePersonInfowithString:(NSString *)str{
    
    UITextField *field1 = [self.view viewWithTag:1000];
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/party/updateMe",CJBaseUrl);
    NSDictionary *dicPar = @{@"name":field1.text,
                             @"headImg":str,
    };
    
    WS(ws);
    [[HDNetworking sharedHDNetworking] POST:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
        
        [ws getUSerInfo];
        
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];

}

- (void)btnLogOutAction:(CJButton *)sender{
    
    WS(ws);
    [self showAlertView:@"退出登录" strContent:@"您确定要退出吗?" ClickOnSure:^(NSString *strSure) {
        [ws logInOut];
    } ClickOnCancle:^(NSString *strCancle) {
        
    }];

}

- (void)logInOut{
    
    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/logout",CJBaseUrl);
    WS(ws);
    NSDictionary *dic = [[NSDictionary alloc]init];
    [[HDNetworking sharedHDNetworking] GET:strUrl parameters:dic success:^(id  _Nonnull responseObject) {

    //               [JPUSHService setAlias:@"" completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
    //                   DLog(@"Alias number %ld",(long)seq);
    //
    //               } seq:101];
    //
    //               NSSet * set2 = [[NSSet alloc] initWithObjects:@"", nil];
    //               [JPUSHService setTags:set2 completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
    //                   DLog(@"Tag number %ld",(long)seq);
    //
    //               } seq:102];

        [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
        DLog(@"Alias number %ld",(long)seq);

        } seq:1];
        [JPUSHService cleanTags:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
        DLog(@"Tag number %ld",(long)seq);

        } seq:2];
        [ws clearAllUserDefaultsData];
        [ws clearCache];
        [ws showLoginWindow];

    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);

    }];
}
    

/*
 自定义方法，清除APP缓存
 */
- (void)clearCache{
    //获取文件管理器
    NSFileManager *fileM = [NSFileManager defaultManager];
    //获取缓存路径
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    //清除缓存
    [fileM removeItemAtPath:cachePath error:nil];

}


#pragma mark ----------lazy
- (NSArray *)arrContent{
    if (!_arrContent) {
        _arrContent = @[CJLString(@"姓名"),CJLString(@"电话"),CJLString(@"点击修改密码")];
    }
    return _arrContent;
}

- (NSArray *)arrTitles{
    if (!_arrTitles) {
        _arrTitles = @[CJLString(@"姓名"),CJLString(@"电话"),CJLString(@"修改密码")];
    }
    return _arrTitles;
}
- (CJButton *)btnLogOut{
    if (!_btnLogOut) {
        _btnLogOut = CJButton.btnInit
        .btnFrame(CGRectMake(0, SCREEN_HEIGHT - LW(44), SCREEN_WIDTH, LW(44)))
        .btnNormalTitleColor([UIColor cjWhiteColor])
        .btnBgColor([UIColor cjMainColor])
        .btnFontSize(17.0)
        .btnText(CJLString(@"退出当前账户"))
        .btnTargetAction(self,@selector(btnLogOutAction:))
        ;
    }
    return _btnLogOut;
}
@end
